package com.example.demo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 詹锦伟
 * @create 2022-09-23 11:32
 *
 * mybatis代码生成器
 */

@RestController
public class CodeGenerator {

    @Autowired
    private static Environment env;

    public static void main(String[] args) {
        String tableNames = "sys_user";
        codeGenerator(tableNames);
    }

    /**
     * 根据传入的数据表名，自动生成代码
     * @param tableName
     */
    public static void codeGenerator(String tableName){

        //创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //全局配置
        GlobalConfig gc = new GlobalConfig();
        //获取项目根目录  E:\JavaWeb\code\myProject\study_vue
        String projectPath = System.getProperty("user.dir");
        //设置输出目录
        gc.setOutputDir(projectPath + "\\java-practice\\src\\main\\java");
        gc.setAuthor("example");
        gc.setActiveRecord(true);
        gc.setIdType(IdType.AUTO);
        gc.setOpen(false);
        gc.setFileOverride(true); //第二次生成会把第一次生成的覆盖掉
        gc.setServiceName("%sService"); //生成的service接口名字首字母是否为I，这样设置就没有
        gc.setBaseResultMap(true); //基本的字段映射

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://localhost:4400/vueadmin?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.example.demo");
//        pc.setModuleName(scanner("模块名"));
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        pc.setEntity("pojo");
        mpg.setPackageInfo(pc);


        // 4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //生成代码的表名
        strategy.setInclude(tableName.split(","));
        // 数据库字段下划线转驼峰命令策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("BaseEntity");
//        strategy.setSuperControllerClass("BaseController");
//        strategy.setSuperEntityColumns("id","created","updated","status");
        // 全局大写命名
        strategy.setCapitalMode(true);
        //数据库表字段映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setRestControllerStyle(true);

        mpg.setStrategy(strategy);

        //执行
        mpg.execute();

    }

}
